package com.wlcorp.forcazamis.dao.pi.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ibatis.dao.client.DaoException;
import com.wlcorp.forcazamis.dao.pi.IPiproductDetailDAO;
import com.wlcorp.forcazamis.domain.pi.Pidetail;
import com.wlcorp.forcazamis.domain.pi.PiproductDetail;
import com.wlcorp.forcazamis.exception.ServiceException;

public class PiproductDetailDAOImpl extends HibernateDaoSupport implements IPiproductDetailDAO{

	public void deletePiproductDetail(PiproductDetail piproductDetail) throws DaoException {
//		piproductDetail.setDeleteFlag(true);
		getHibernateTemplate().delete(piproductDetail);
	}

	public void savePiproductDetail(PiproductDetail piproductDetail) throws DaoException {
		getHibernateTemplate().saveOrUpdate(piproductDetail);
	}

	public PiproductDetail getPiproductDetailById(Long productId) throws DaoException {
		return (PiproductDetail)getHibernateTemplate().get(PiproductDetail.class,productId);
	}

	@SuppressWarnings("unchecked")
	public List getPiproductDetailList(final PiproductDetail piproductDetail) throws DaoException {
		final StringBuffer queryString = new StringBuffer("select piproductDetail from PiproductDetail as piproductDetail where 1=1 and piproductDetail.deleteFlag=false");
		if(null != piproductDetail.getPiid()){
			queryString.append(" and piproductDetail.pidetail.piid =").append(piproductDetail.getPiid());
		}
		if(null != piproductDetail.getCustomerId()){
			queryString.append(" and piproductDetail.pidetail.customer.customerId =").append(piproductDetail.getCustomerId());
		}
		queryString.append(" order by piproductDetail.orderNo asc");
		return getHibernateTemplate().executeFind(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(queryString.toString());
				if(null != piproductDetail && null != piproductDetail.getMinRownum()){
					query.setFirstResult(piproductDetail.getMinRownum());
					query.setMaxResults(piproductDetail.getNumPerPage());
				}
				return query.list();
			}
			
		});
	}
	
	@SuppressWarnings("unchecked")
	public List getPiproductDetailListByCode(final PiproductDetail piproductDetail) throws DaoException {
			final StringBuffer queryString = new StringBuffer("select piproductDetail from PiproductDetail as piproductDetail where 1=1 and piproductDetail.deleteFlag=false");
			if(null != piproductDetail.getCustomerId()){
				queryString.append(" and piproductDetail.pidetail.customer.customerId =").append(piproductDetail.getCustomerId());
			}
			if(!piproductDetail.getCustProdCode().equals("")&&!piproductDetail.getSubSample().getParentSample().getSampleCode().equals("")){
				queryString.append(" and (piproductDetail.custProdCode like '%").append(piproductDetail.getCustProdCode()).append("%'");
				queryString.append(" or piproductDetail.subSample.parentSample.sampleCode like '%").append(piproductDetail.getSubSample().getParentSample().getSampleCode()).append("%')");
			}
			if(!piproductDetail.getCustProdCode().equals("")&&piproductDetail.getSubSample().getParentSample().getSampleCode().equals("")){
				queryString.append(" and piproductDetail.custProdCode like '%").append(piproductDetail.getCustProdCode()).append("%'");
			}
			if(piproductDetail.getCustProdCode().equals("")&&!piproductDetail.getSubSample().getParentSample().getSampleCode().equals("")){
				queryString.append(" and piproductDetail.subSample.parentSample.sampleCode like '%").append(piproductDetail.getSubSample().getParentSample().getSampleCode()).append("%'");
			}
			queryString.append(" order by piproductDetail.orderNo asc");
			return getHibernateTemplate().executeFind(new HibernateCallback(){
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					Query query = session.createQuery(queryString.toString());
					if(null != piproductDetail && null != piproductDetail.getMinRownum()){
						query.setFirstResult(piproductDetail.getMinRownum());
						query.setMaxResults(piproductDetail.getNumPerPage());
					}
					return query.list();
				}
			});
		}

	@SuppressWarnings("unchecked")
	public List getPiproductDetailListByPidetailId(Long piid)
			throws ServiceException {
		return getHibernateTemplate()
				.find("from PiproductDetail as piproductDetail where 1=1 and piproductDetail.deleteFlag=false and piproductDetail.pidetail.piid ="+ piid);
	}

	public void updatePidetail(Pidetail pidetail) throws DaoException {
		getHibernateTemplate().update(pidetail);
	}

}
